// pages/client/client.js
let conf = require('../../app.config').app
const systemInfo = wx.getSystemInfoSync();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bg_height:0,
    id:0,
    set_info_region_customItem: '全部',
    set_info_region: ['省', '市', '区'],
    region:'',
    realname:'',
    mobile:'',
    address:'',
    isDefault:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let t = this;
    var w = systemInfo.windowWidth;
    var h = systemInfo.windowHeight;
    t.setData({
      bg_height:(750 / 750) * w,
    });
    if(options.id){
      t.api.interface.getCustomerAddress(options.id).then(res => {
        if(res.data.code == 200){
          let region = res.data.data.region;
          let arr_region = region.split(",");
          t.setData({
            id:res.data.data.id,
            realname: res.data.data.realname,
            mobile: res.data.data.mobile,
            address: res.data.data.address,
            set_info_region:[arr_region[0],arr_region[1],arr_region[2]],
            region:region,
            isDefault:res.data.data.isDefault,
          });

        }
      });

    }

  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  cli_addresslist_back(){
    let t = this;

    wx.chooseAddress({
      success (res) {
        console.log(res);

        t.setData({
          realname: res.userName,
          mobile: res.telNumber,
          address: res.detailInfo,
          set_info_region:[res.provinceName,res.cityName,res.countyName],
          region:res.provinceName+","+res.cityName+","+res.countyName,
        })

      }
    })
  },

  cli_dingwei(){
    console.log(33333)
    let t = this;
    wx.chooseLocation({
      success(res){
        console.log(res);
        t.setData({
          address: res.address
        })
      },error(res){
        console.log(res);
      },complete(res){
        console.log(res);
      }
    });
  },
  cli_tijiao(){
    let t = this;
    if(t.data.realname == ""){
      wx.showModal({
        title: '提示',
        content: '请填写收货人姓名',
        showCancel: false,
      });
      return false;
    }
    if(t.data.mobile == ""){
      wx.showModal({
        title: '提示',
        content: '请填写收货人手机号码',
        showCancel: false,
      });
      return false;
    }
    if(t.data.region == ""){
      wx.showModal({
        title: '提示',
        content: '请选择所在区域',
        showCancel: false,
      });
      return false;
    }
    if(t.data.address == ""){
      wx.showModal({
        title: '提示',
        content: '请填写收货地址',
        showCancel: false,
      });
      return false;
    }
    let p = {
      realname:t.data.realname,
      mobile:t.data.mobile,
      region:t.data.region,
      address:t.data.address,
      isDefault:t.data.isDefault
    };
    if(t.data.id == 0){
      t.api.interface.addCustomerAddress(p).then(res => {
        if(res.data.code == 200){
          wx.showModal({
            title: '提示',
            content: '添加成功',
            showCancel: false,
            success:function(res){
              wx.navigateBack({
                delta: 1
              });
            }
          });
        }
      });
    }else{
      p.id = t.data.id;
      t.api.interface.updateCustomerAddress(p).then(res => {
        if(res.data.code == 200){
          wx.showModal({
            title: '提示',
            content: '修改成功',
            showCancel: false,
            success:function(res){
              wx.navigateBack({
                delta: 1
              });
            }
          });
        }
      });
    }
  },
  bindSetInfoRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    let region = e.detail.value.join(",");
    this.setData({
      set_info_region: e.detail.value,
      region:region,
    })
  },
  change_realname:function(e){
    this.setData({
      realname: e.detail.value
    });
  },
  change_mobile:function(e){
    this.setData({
      mobile: e.detail.value
    });
  },
  change_address:function(e){
    this.setData({
      address: e.detail.value
    });
  },
  cli_default(){
    let t = this;
    console.log(t.data.isDefault);
    if(t.data.isDefault == 0){
      t.setData({
        isDefault: 1
      });
    }else{
      t.setData({
        isDefault: 0
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})